<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Catalog category
 *
 * @category   Mage
 * @package    Mage_Catalog
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Verz_Home_Model_Catalog_Category extends Mage_Catalog_Model_Category
{   
    
     public function getChildrenCates($category_id, $page=false){
        
        $collection = Mage::getModel('home/catalog_category')->getCollection();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
        $collection->addAttributeToSelect('name')
            ->addAttributeToSelect('description')
            ->addAttributeToSelect('image')
            ->addAttributeToSelect('is_active')
            ->addAttributeToSelect('url_key')
            ->addAttributeToSelect('parent_id')
                
            ->addAttributeToFilter('parent_id',$category_id)   
            ->addIsActiveFilter();
        if(!empty($page))
            $collection->setPage($page, DEFAULT_NUMBER_CATE_MIX_PAGE);
        
                
        /*        
        if ($sorted) {
            if (is_string($sorted)) {
                // $sorted is supposed to be attribute name
                $collection->addAttributeToSort($sorted);
            } else {
                $collection->addAttributeToSort('name');
            }
        }
        */
        return $collection;
    }
     public function getPagination($category_id,$page_number){
        
        $total = $this->countCategoryList($category_id);
        
        $last = intval(ceil($total/DEFAULT_NUMBER_CATE_MIX_PAGE)); 
        
        //this makes sure the page number isn't below one, or more than our maximum pages 
        if ($page_number < 1) 
        { 
            $page_number = 1; 
        } 
        elseif ($page_number > $last) 
        { 
            $page_number = $last; 
        } 
        //This sets the range to display in our query 
        return array("total"=>$total,
                     "page_number"=>intval($page_number),
                     "last"=>$last,
                     "from"=>($page_number - 1) * DEFAULT_NUMBER_CATE_MIX_PAGE,
                     "to"=>$page_number * DEFAULT_NUMBER_CATE_MIX_PAGE);
        
    }    
    public function countCategoryList($category_id){
        
        $collection = Mage::getModel('home/catalog_category')->getCollection();
        /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
        $total = $collection->addAttributeToSelect('count(*)')
           
            ->addAttributeToFilter('parent_id',$category_id)   
            ->addIsActiveFilter()->getSize();
        
        return $total;
    }
}
